package com.jzoom.zoom.admin.models;

import com.jzoom.zoom.common.annotations.Inject;
import com.jzoom.zoom.dao.Dao;
import com.jzoom.zoom.dao.Record;
import com.jzoom.zoom.web.view.impl.StringView;

import java.util.List;

public class ComponentsService {

	@Inject(value = "admin")
	private Dao admin;
	
	private static final String FORMAT = "Vue.component('%s',{template:`%s`});\n";
	private static final String DATE = "Vue.component('%s',%s);\n";
	/**
	 * 获取全部的组件
	 * @return
	 */
	public StringView getComponents() {
		List<Record> records = admin.table("sys_component").find();
		StringBuilder sb = new StringBuilder();
		for (Record record : records) {
			String template = record.getString("template");
			if(template==null)continue;;
			template =template.trim();
			if(template.startsWith("<") && template.endsWith(">")){
				sb.append(String.format(FORMAT, record.get("name"),template));
			}else if(template.startsWith("{") && template.endsWith("}")){
				sb.append(String.format(DATE, record.get("name"),template));
			}

		}
		return new StringView(sb.toString());
		
	}

	public int getLastUpdate() {
		Record record = admin.table("sys_component").selectMax("update_at").get();
		if(record == null)return 0;
		return record.getInt("update_at");
	}
	
}
